<?php

class cargo_packing_list extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'cargo_packing_list':
	 * @var integer $id
	 * @var integer $do_details_id
	 * @var string $item
	 * @var integer $qty
	 * @var integer $qty_type
	 */
         public $qty,$qty_type;
         public $type=array('pcs','roll','pkg','lot','sheet');
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cargo_packing_list';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
                        array('item, marking, country','length','max'=>50),
			array('qty_id, cargo_id, id, qty, qty_type', 'numerical', 'integerOnly'=>true),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'qty'=>array(self::BELONGS_TO,'cargo_qty','qty_id'),
                   // 'our_marking'=>array(self::BELONGS_TO,'cargo_details','do_details_id'),
		);
	}
        public function beforeSave() {
            $cargo_qty = new cargo_qty;
            if($cargo_qty->exists("qty=:qty AND qty_type=:qty_type", array(":qty"=>$this->qty,":qty_type"=>$this->qty_type))) {
                $this->qty_id = $cargo_qty->find("qty=:qty AND qty_type=:qty_type", array(":qty"=>$this->qty,":qty_type"=>$this->qty_type))->id;
            }
            else {
                $cargo_qty->qty = $this->qty;
                $cargo_qty->qty_type = $this->qty_type;
                if(!$cargo_qty->save())
                    return false;
                $this->qty_id = $cargo_qty->id;
            }
            if(empty($this->country))
                $this->country = "Singapore";
            return true;
        }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'item' => 'Item',
                        'country'=>'Origin',
			'qty' => 'Qty',
			'qty_type' => 'Qty Type',
		);
	}
}